﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    public class MenuDataSeederContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<Menu, Guid> _systemMenuRepository;

        public MenuDataSeederContributor(IRepository<Menu, Guid> systemMenuRepository)
        {
            _systemMenuRepository = systemMenuRepository;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            //var defaultRole = "admin";
            //var menuQuery = await _systemMenuRepository.GetQueryableAsync();

            //#region 设置

            //var settingMenu = menuQuery.FirstOrDefault(m => m.MenuCodg == "Setting");

            //if (settingMenu == null)
            //{
            //    settingMenu = new Menu();
            //    settingMenu.MenuCodg = "Setting";
            //    settingMenu.MenuName = "设置";
            //    settingMenu.GrpFlag = true;
            //    settingMenu.HideInBreadcrumbFlag = true;
            //    settingMenu.ApplicationId = string.Empty;
            //    settingMenu = await _systemMenuRepository.InsertAsync(settingMenu);
            //}

            //const string SettingSys = "SettingSys";
            //var systemSettingMenu = menuQuery.FirstOrDefault(m => m.MenuCodg == SettingSys);

            //if (systemSettingMenu == null)
            //{
            //    systemSettingMenu = new Menu();
            //    systemSettingMenu.MenuCodg = SettingSys;
            //    systemSettingMenu.MenuName = "系统设置";
            //    systemSettingMenu.GrpFlag = true;
            //    systemSettingMenu.HideInBreadcrumbFlag = false;
            //    systemSettingMenu.Icon = "anticon-appstore";
            //    systemSettingMenu.Parent = settingMenu;
            //    systemSettingMenu.ApplicationId = string.Empty;
            //    systemSettingMenu = await _systemMenuRepository.InsertAsync(systemSettingMenu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = systemSettingMenu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"AbpIdentity.Users"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"AbpIdentity.Users";
            //    menu.MenuName = "用户管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/user-manage";
            //    menu.Srtno = 1;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"AbpIdentity.Roles"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"AbpIdentity.Roles";
            //    menu.MenuName = "角色管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/role-manage";
            //    menu.Srtno = 2;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"AbpTenantManagement.Tenants"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"AbpTenantManagement.Tenants";
            //    menu.MenuName = "租户管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/tenant-manage";
            //    menu.Srtno = 3;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Menu"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Menu";
            //    menu.MenuName = "菜单管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/menu-manage";
            //    menu.Srtno = 4;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Organ"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Organ";
            //    menu.MenuName = "机构管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/organ-manage";
            //    menu.Srtno = 5;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Dept"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Dept";
            //    menu.MenuName = "部门管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/dept-manage";
            //    menu.Srtno = 6;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Staff"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Staff";
            //    menu.MenuName = "人员管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Link = @"/sys/psn-manage";
            //    menu.Parent = systemSettingMenu;
            //    menu.Srtno = 7;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Pos"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Pos";
            //    menu.MenuName = "岗位管理";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Link = @"/sys/pos-manage";
            //    menu.Parent = systemSettingMenu;
            //    menu.Srtno = 7;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Auditlog"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Auditlog";
            //    menu.MenuName = "审计日志";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/audit-log";
            //    menu.Srtno = 9;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.Syslog"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.Syslog";
            //    menu.MenuName = "系统日志";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/sys-log";
            //    menu.Srtno = 10;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.DataDictionary"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.DataDictionary";
            //    menu.MenuName = "数据字典";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/data-dict";
            //    menu.Srtno = 8;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.CatgeDict"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.CatgeDict";
            //    menu.MenuName = "分类字典";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/catge-dict";
            //    menu.Srtno = 8;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.MsgRec"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.MsgRec";
            //    menu.MenuName = "消息记录";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/msg-rend-rec";
            //    menu.Srtno = 12;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.MsgType"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.MsgType";
            //    menu.MenuName = "消息类别";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/msg-biz-type";
            //    menu.Srtno = 11;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{SettingSys}.NoDict"))
            //{
            //    Menu menu = new Menu();
            //    menu.MenuCodg = $"{SettingSys}.NoDict";
            //    menu.MenuName = "编号字典";
            //    menu.GrpFlag = false;
            //    menu.HideInBreadcrumbFlag = false;
            //    menu.Parent = systemSettingMenu;
            //    menu.Link = @"/sys/codg-config";
            //    menu.Srtno = 11;
            //    menu.ApplicationId = string.Empty;
            //    menu = await _systemMenuRepository.InsertAsync(menu);

            //    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //    {
            //        RoleCode = defaultRole,
            //        MenuId = menu.Id
            //    });
            //}

            //#endregion

            //await SeedJsonAsync(LowCodeJson);
        }

        public async Task SeedJsonAsync(string menuJson)
        {
            //var convMenus = Newtonsoft.Json.JsonConvert.DeserializeObject<List<MenuSeedJson>>(menuJson);
            //var defaultRole = "admin";
            //var menuQuery = await _systemMenuRepository.GetQueryableAsync();

            //foreach (var menuLevel1 in convMenus)
            //{
            //    var lv1Menu = menuQuery.FirstOrDefault(m => m.MenuCodg == menuLevel1.menuCode);

            //    if (lv1Menu == null)
            //    {
            //        lv1Menu = new Menu();
            //        lv1Menu.MenuCodg = menuLevel1.menuCode;
            //        lv1Menu.MenuName = menuLevel1.text;
            //        lv1Menu.ApplicationId = menuLevel1.ApplicationId;
            //        lv1Menu.GrpFlag = true;
            //        lv1Menu.HideInBreadcrumbFlag = true;
            //        lv1Menu = await _systemMenuRepository.InsertAsync(lv1Menu);
            //    }

            //    foreach (var menuLevel2 in menuLevel1.children)
            //    {
            //        var lv2Menu = menuQuery.FirstOrDefault(m => m.MenuCodg == menuLevel2.menuCode);

            //        if (lv2Menu == null)
            //        {
            //            lv2Menu = new Menu();
            //            lv2Menu.ApplicationId = menuLevel2.ApplicationId;
            //            lv2Menu.MenuCodg = menuLevel2.menuCode;
            //            lv2Menu.MenuName = menuLevel2.text;
            //            lv2Menu.GrpFlag = true;
            //            lv2Menu.HideInBreadcrumbFlag = false;
            //            lv2Menu.Icon = "anticon-appstore";
            //            lv2Menu.Parent = lv1Menu;
            //            lv2Menu = await _systemMenuRepository.InsertAsync(lv2Menu);

            //            await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //            {
            //                RoleCode = defaultRole,
            //                MenuId = lv2Menu.Id
            //            });
            //        }

            //        foreach (var menuLevel3 in menuLevel2.children)
            //        {
            //            var lv3Menu = menuQuery.FirstOrDefault(m => m.MenuCodg == menuLevel3.menuCode);

            //            if (lv3Menu == null)
            //            {
            //                lv3Menu = new Menu();
            //                lv3Menu.ApplicationId = menuLevel3.ApplicationId;
            //                lv3Menu.MenuCodg = menuLevel3.menuCode;
            //                lv3Menu.MenuName = menuLevel3.text;
            //                lv3Menu.GrpFlag = true;
            //                lv3Menu.HideInBreadcrumbFlag = false;
            //                lv3Menu.Link = menuLevel3.link;
            //                lv3Menu.Icon = "anticon-appstore";
            //                lv3Menu.Parent = lv2Menu;
            //                lv3Menu = await _systemMenuRepository.InsertAsync(lv3Menu);

            //                await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //                {
            //                    RoleCode = defaultRole,
            //                    MenuId = lv3Menu.Id
            //                });
            //            }

            //            foreach (var menuLevel4 in menuLevel3.children)
            //            {
            //                if (!await _systemMenuRepository.AnyAsync(m => m.MenuCodg == $"{menuLevel3.menuCode}.{menuLevel4.menuCode}"))
            //                {
            //                    Menu menu = new Menu();
            //                    menu.ApplicationId = menuLevel4.ApplicationId;
            //                    menu.MenuCodg = $"{menuLevel3.menuCode}.{menuLevel4.menuCode}";
            //                    menu.MenuName = menuLevel4.text;
            //                    menu.GrpFlag = false;
            //                    menu.HideInBreadcrumbFlag = false;
            //                    menu.Parent = lv3Menu;
            //                    menu.Link = menuLevel4.link;
            //                    menu.Srtno = 1;
            //                    menu = await _systemMenuRepository.InsertAsync(menu);

            //                    await _roleAsMenuRepository.InsertAsync(new MenuAclRole()
            //                    {
            //                        RoleCode = defaultRole,
            //                        MenuId = menu.Id
            //                    });
            //                }
            //            }
            //        }
            //    }
            //}

        }

        public class MenuSeedJson
        {
            public string menuCode { get; set; }

            public string text { get; set; }

            public string ApplicationId { get; set; }

            public string? link { get; set; }

            public IList<MenuSeedJson> children { get; set; }
        }

        public const string LowCodeJson = @"
        [
            {
            ""menuCode"":""LowCode"",
            ""text"": ""Low-Code"",
            ""ApplicationId"": """",
            ""srtno"": 0,
            ""children"": [
                {
                    ""menuCode"":""LowCodgBaseSetting"",
                    ""text"": ""基础设置"",
                    ""ApplicationId"": """",
                    ""srtno"": 100,
                    ""children"": [
                    {
                        ""menuCode"": ""LowCodgBaseSetting.ProjectSchemaConfig"",
                        ""text"": ""项目空间"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/project-schema-config"",
                        ""srtno"": 110,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""LowCodgBaseSetting.ProjectConfig"",
                        ""text"": ""项目信息"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/project-config"",
                        ""srtno"": 120,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""LowCodgBaseSetting.DataTableConfig"",
                        ""text"": ""表格配置"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/data-table-config"",
                        ""srtno"": 130,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""LowCodgBaseSetting.OpenApiConfig"",
                        ""text"": ""接口配置"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/open-api-config"",
                        ""srtno"": 140,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""LowCodgBaseSetting.DataFormConfig"",
                        ""text"": ""表单配置"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/data-form-config"",
                        ""srtno"": 150,
                        ""children"": []
                    }]
                },
                {
                    ""menuCode"":""SettingDict"",
                    ""text"": ""元数据"",
                    ""ApplicationId"": """",
                    ""srtno"": 200,
                    ""children"": [
                    {
                        ""menuCode"": ""SettingDict.StdWordDict"",
                        ""text"": ""标准单词"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/std-word"",
                        ""srtno"": 210,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""SettingDict.StdDomainDict"",
                        ""text"": ""标准域"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/std-domain"",
                        ""srtno"": 220,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""SettingDict.StdPhrDict"",
                        ""text"": ""标准用语"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/std-phr"",
                        ""srtno"": 230,
                        ""children"": []
                    }]
                },
                {
                    ""menuCode"":""WorkflowManage"",
                    ""text"": ""工作流"",
                    ""ApplicationId"": """",
                    ""srtno"": 200,
                    ""children"": [
                    {
                        ""menuCode"": ""WorkflowManage.ProcessDefinition"",
                        ""text"": ""流程定义"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/process-definition"",
                        ""srtno"": 110,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""WorkflowManage.ProcessType"",
                        ""text"": ""流程类别"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/process-type"",
                        ""srtno"": 120,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""WorkflowManage.Node"",
                        ""text"": ""流程节点"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/process-node"",
                        ""srtno"": 120,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""WorkflowManage.ProcessInstance"",
                        ""text"": ""流程实例"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/process-instance"",
                        ""srtno"": 120,
                        ""children"": []
                    },
                    {
                        ""menuCode"": ""WorkflowManage.Todo"",
                        ""text"": ""待办记录"",
                        ""ApplicationId"": """",
                        ""link"": ""/lowcode/workflow-to"",
                        ""srtno"": 120,
                        ""children"": []
                    }]
                }]
            }
        ]";
    }
}
